package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;
import cn.itcast.bos.web.action.common.BaseAction;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope("prototype")
public class StandardAction extends BaseAction<Standard> {

	@Autowired
	private StandardService standardService;

	@Action(value = "standard_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/standard.html") })
	public String save() {
		standardService.save(model);
		return SUCCESS;
	}

	@Action(value = "standard_showPage")
	public void showPage() {
		Pageable pageable = new PageRequest(page, rows);
		Page<Standard> pageData = standardService.showPage(pageable);
		writeJson(pageData);
	}

	@Action(value = "standard_findAll")
	public void findAll() {
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		List<Standard> list = standardService.findAll();
		String json = JSONArray.toJSONString(list);
		//添加默认选中selected
//		int i = json.indexOf('"' + "id" + '"' + ":37");
//		String a = json.substring(0, i);
//		String b = json.substring(i);
//		String c = '"' + "selected" + '"' + ":true,";
//		String newJson = a + c + b;
		try {
			ServletActionContext.getResponse().getWriter().write(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
